# @Version        : 1.0
# @Update Time    : 2025/3/24 22:42
# @File           : track_utils.py
# @IDE            : PyCharm
# @Desc           : 链路追踪上下文管理器
# 存储请求上下文存储器
import contextvars
from typing import Optional

_request_id_var: contextvars.ContextVar[Optional[str]] = contextvars.ContextVar("request_id", default=None)


class TrackContextUtils:
    @staticmethod
    def get_request_id() -> Optional[str]:
        return _request_id_var.get()

    @staticmethod
    def set_request_id(rid: str) -> str:
        _request_id_var.set(rid)
        return rid

    @staticmethod
    def clear_request_id() -> None:
        _request_id_var.set(None)
